﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Extensions.Internal;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using WebApp.Services;

namespace WebApp.Controllers
{
    [Produces("application/json")]
    [Route("api/EfSqlLite")]
    public class EfSqlLiteController : Controller
    {
        private readonly ILogger<EfSqlLiteController> _logger;
        private readonly EfSqlLiteContextService _sqlLiteContextService;


        public EfSqlLiteController(ILogger<EfSqlLiteController> logger, EfSqlLiteContextService sqlLiteContextService)
        {
            _logger = logger;
            _sqlLiteContextService = sqlLiteContextService;
        }


        // GET api/values/id
        [HttpGet("{id}")]
        public string Get(int id)
        {
            var m = _sqlLiteContextService.TestModels.FirstOrDefault(c => c.Id == id);
            return JsonConvert.SerializeObject(m);

            //// 分页
            //var l = _sqlLiteContextService.TestModels.Where(c=>c.Id>2 && c.Id<5).OrderByDescending(c => c.Id)
            //    .Skip(1).Take(2);
            //return JsonConvert.SerializeObject(l.ToList());
        }


        // POST api/values
        [HttpPost]
        public void Post([FromBody]int key)
        {



            var m = _sqlLiteContextService.TestModels.Add(new TestModel { Id = key, Name = DateTime.Now.ToShortTimeString() });
            _sqlLiteContextService.SaveChanges();
        }



    }
}